package com.dawn.module.cms.convert.article;

import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.util.collection.CollectionUtils;
import com.dawn.framework.common.util.object.BeanUtils;
import com.dawn.module.cms.controller.admin.article.vo.ArticleRespVO;
import com.dawn.module.cms.controller.admin.article.vo.ArticleSaveReqVO;
import com.dawn.module.cms.controller.app.article.vo.AppArticleRespVO;
import com.dawn.module.cms.dal.dataobject.article.CmsArticleDO;
import com.dawn.module.cms.dal.dataobject.category.CategoryDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

/**
 * 文章 Convert
 *
 * @author yangyuhua
 */
@Mapper
public interface ArticleConvert {

    default List<ArticleRespVO> convertList(List<CmsArticleDO> list, Map<Long, CategoryDO> categoryMap) {
        return CollectionUtils.convertList(list, article -> convert(article, categoryMap.get(article.getCategoryId())));
    }

    default ArticleRespVO convert(CmsArticleDO user, CategoryDO categoryDO) {
        ArticleRespVO articleRespVO = BeanUtils.toBean(user, ArticleRespVO.class);
        if (categoryDO != null) {
            articleRespVO.setCategoryKey(categoryDO.getCategoryKey());
            articleRespVO.setCategoryName(categoryDO.getName());
        }
        return articleRespVO;
    }

    default List<AppArticleRespVO> convertList0(List<CmsArticleDO> list) {
        return CollectionUtils.convertList(list, article -> convert0(article));
    }

    default AppArticleRespVO convert0(CmsArticleDO articleDO) {
        AppArticleRespVO articleRespVO = BeanUtils.toBean(articleDO, AppArticleRespVO.class);
        if(articleDO.getTags()!=null){
            String[] tagsArray = articleDO.getTags().split(",");
            articleRespVO.setTagsArray(tagsArray);
        }
        return articleRespVO;
    }

    ArticleConvert INSTANCE = Mappers.getMapper(ArticleConvert.class);

    CmsArticleDO convert(ArticleSaveReqVO bean);


    ArticleRespVO convert(CmsArticleDO bean);

    List<ArticleRespVO> convertList(List<CmsArticleDO> list);

    PageResult<ArticleRespVO> convertPage(PageResult<CmsArticleDO> page);


}
